﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using MouseExtender.Resources;
using MouseExtender.Logic.Common;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using PreEmptive.Attributes;


namespace MouseExtender.Logic.Managers
{
    public static class ResourceManager
    {
        private static readonly CultureInfo _defaultCulture = new CultureInfo("en-US");
        //use hardcoded list instead of all cultures, as all cultures takes to much memory
        private static readonly List<CultureInfo> _availableCultures = new List<CultureInfo>()
                {
                    new CultureInfo("ar"),
                    new CultureInfo("be-BY"),
                    new CultureInfo("bg-BG"),
                    new CultureInfo("cs-CZ"),
                    new CultureInfo("da-DK"),
                    new CultureInfo("de-DE"),
                    new CultureInfo("el-GR"),
                    new CultureInfo("es-ES"),
                    new CultureInfo("fr-FR"),
                    new CultureInfo("ja-JP"),
                    new CultureInfo("it-IT"),
                    new CultureInfo("nl-NL"),
                    new CultureInfo("pl-PL"),
                    new CultureInfo("pt-PT"),
                    new CultureInfo("ru-RU"),
                    new CultureInfo("tr-TR"),
                    new CultureInfo("uk-UA"),
                    new CultureInfo("zh-CN"),
                    new CultureInfo("zh-TW"),
                };

        public static List<CultureInfo> AvailableCultures
        {
            get
            {
                DirectoryInfo folder = new DirectoryInfo(Application.StartupPath);
                DirectoryInfo[] resourceFolders = folder.GetDirectories();
                var folderNames = resourceFolders.Select(dir => dir.Name.ToLower());
                List<CultureInfo> cultures = _availableCultures.Where(culture => folderNames.Contains(culture.Name.ToLower())).ToList();
                if (!cultures.Contains(_defaultCulture))
                    cultures.Add(_defaultCulture);
                return cultures;
            }
        }

        public static CultureInfo CurrentCulture
        {
            get
            {
                return UIResources.Culture;
            }
        }

        private static void SetCultureInternal(CultureInfo culture, bool saveSettings)
        {
            UIResources.Culture = culture;
            if (saveSettings)
            {
                SettingsManager.CurrentSettings.UICulture = culture.Name;
                SettingsManager.CurrentSettings.SettingChanged();
                SingleInstanceManager.RestartApplication();
            }
        }

        public static void SetCulture()
        {
            string uiCulture = SettingsManager.CurrentSettings.UICulture;
            CultureInfo culture = CultureInfo.CurrentCulture;
            if (!String.IsNullOrEmpty(uiCulture))
                try
                {
                    culture = new CultureInfo(uiCulture);
                }
                catch (ArgumentException ex)
                {
                    Logger.HandleNonCriticalError(ex);
                }
            SetCultureInternal(culture, false);
        }

        public static void SetCulture(CultureInfo culture)
        {
            SetCultureInternal(culture, true);
        }

        public static bool IsCurrentCulture(CultureInfo cultureInfo)
        {
            return CurrentCulture.Equals(cultureInfo);
        }

        public static string GetUIResourceString(string key)
        {
            return UIResources.ResourceManager.GetString(key, UIResources.Culture);
        }

        public static string GetMessage(string key, params object[] stringParameters)
        {
            string message = String.Empty;
            string messageTemplate = GetUIResourceString(key);
            if (!String.IsNullOrEmpty(messageTemplate))
                message = String.Format(messageTemplate, stringParameters);

            return message;
        }
    }
}
